package com.bootdo.program.wallpaper.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.bootdo.common.thirdparty.ali.AlipayInfoService;
import com.bootdo.common.utils.seq.GenerateSequenceUtil;
import com.bootdo.program.wallpaper.dao.TzUserDao;
import com.bootdo.program.wallpaper.dao.WalletDetailsDao;
import com.bootdo.program.wallpaper.domain.CollectionInfoDO;
import com.bootdo.program.wallpaper.domain.TzUserDO;
import com.bootdo.program.wallpaper.domain.WalletDetailsDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.program.wallpaper.dao.WithdrawalDetailsDao;
import com.bootdo.program.wallpaper.domain.WithdrawalDetailsDO;
import com.bootdo.program.wallpaper.service.WithdrawalDetailsService;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(rollbackFor = Exception.class)
public class WithdrawalDetailsService {
	@Autowired
	private WithdrawalDetailsDao withdrawalDetailsDao;

	@Autowired
	private TzUserDao tzUserDao;

	@Autowired
	private WalletDetailsService walletDetailsService;

	@Autowired
	private CollectionInfoService collectionInfoService;

	@Autowired
	private AlipayInfoService alipayInfoService;

	@Value("${pay.ali.closePay}")
	private boolean closePay;

	public WithdrawalDetailsDO get(Long id){
		return withdrawalDetailsDao.get(id);
	}

	public WithdrawalDetailsDO getById(Long id){
		return withdrawalDetailsDao.getById(id);
	}

	public List<WithdrawalDetailsDO> list(Map<String, Object> map){
		return withdrawalDetailsDao.list(map);
	}


	public int count(Map<String, Object> map){
		return withdrawalDetailsDao.count(map);
	}


	public int save(WithdrawalDetailsDO withdrawalDetails){
		return withdrawalDetailsDao.save(withdrawalDetails);
	}


	public int update(WithdrawalDetailsDO withdrawalDetails){
		return withdrawalDetailsDao.update(withdrawalDetails);
	}


	public int remove(Long id){
		return withdrawalDetailsDao.remove(id);
	}


	public int batchRemove(Long[] ids){
		return withdrawalDetailsDao.batchRemove(ids);
	}


	/**
	 * 修改提现功能
	 * @param withdrawalDetails
	 * @return
	 */
	public int updateTx(WithdrawalDetailsDO withdrawalDetails) throws AlipayApiException {

		Date date = new Date();

		WithdrawalDetailsDO detailsDO = withdrawalDetailsDao.get(withdrawalDetails.getId());

		if(null == detailsDO){
			throw new RuntimeException("获取提现信息失败");
		}

		detailsDO.setRemark(withdrawalDetails.getRemark());
		detailsDO.setStatus(withdrawalDetails.getStatus());
		detailsDO.setAuditTime(date);
		if(2 == withdrawalDetails.getStatus()){
			detailsDO.setPayTime(date);
			//walletDetailsService.addWallet(detailsDO.getUserId(),detailsDO.getMoney(),1,2);

			//支付宝打款给用户
			CollectionInfoDO collectionInfoDO = collectionInfoService.get(detailsDO.getCollectionId());
			if(collectionInfoDO.getPayType() == 2){
				//调用支付宝转账接口,false 进入支付宝打款
				if(!closePay){
					AlipayFundTransUniTransferResponse response = alipayInfoService.alipayFundTransUniTransferRequest("抖音/快手星光壁纸提现",
							collectionInfoDO.getAccount(), collectionInfoDO.getFullName(),
							detailsDO.getActualMoney().stripTrailingZeros().toPlainString(), "抖音/快手星光壁纸提现");
					//请求成功
					if(response.isSuccess()){
						//处理业务
						detailsDO.setTransDate(response.getTransDate());
						detailsDO.setCallbackStatus(response.getStatus());
						detailsDO.setOrderId(response.getOrderId());
						detailsDO.setPayFundOrderId(response.getPayFundOrderId());
					}else{
						detailsDO.setRemark(response.getSubMsg());
						detailsDO.setStatus(3);
						//回退用户金额
						rollbackMoney(detailsDO);
					}

					int update = withdrawalDetailsDao.update(detailsDO);
					return update;
				}
			}

			int update = withdrawalDetailsDao.update(detailsDO);
			return update;
		}else if(3 == withdrawalDetails.getStatus()){
			int update = withdrawalDetailsDao.update(detailsDO);
			rollbackMoney(detailsDO);
			return update;
		}
		return 0;
	}

	/**
	 * 回滚用户金额
	 * @param detailsDO
	 */
	public void rollbackMoney(WithdrawalDetailsDO detailsDO){
		//审核不通过  回滚用户金额数据
		TzUserDO userDO = tzUserDao.getByUserId(detailsDO.getUserId());
		if(null == userDO){
			throw new RuntimeException("获取用户信息失败");
		}
		BigDecimal money = detailsDO.getMoney();

		userDO.setBalance(userDO.getBalance().add(money));
		tzUserDao.update(userDO);

		//添加钱包明细
		walletDetailsService.addWallet(detailsDO.getUserId(),detailsDO.getMoney(),1,1);

	}




}
